﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Entity;
using NLBS.Framework;
using NLBS.Text;
using NLBS.Views;
using NLBS.Data.Business;

namespace NLBS.Validate
{
   public class TrackBackValidate:ValidateTemplete<TrackBack>
    {
       public void trackbackResponse(int errorNum, string error)
       {
       }

 


       public override void checkPower()
       {
           switch (Act)
           {
               case Action.Insert:

                   break;
               case Action.Delete:
                   if (power.Delete < 1)
                       throw new Exception("没有权限进行此操作");
                   break;
           }

       }

       public TrackBackValidate(Action act, int keyID)
        {
            this.Act = act;
            this.keyID = keyID;
        }

        public override void checkArgument()
        {
            throw new NotImplementedException();
        }

        public override void checkEntity()
        {
            if (this.Entity == null)
            {
                throw new Exception("引用不存在");
            }
        }

        public override void checkPowerAgain()
        {
            User currentUser = BlogInfo.Instance().CurrentUser;
            switch (Act)
            {
                case Action.Delete:
                    if (currentUser.UserID != this.Entity.Article.AuthorID && power.Delete < 2)
                    {
                        throw new Exception("没有权限进行此操作");
                    }
                    break;
            }
        }

        public void ValidateDelete(CallValid valid, CallError error)
        {
            try
            {
                checkPower();
                checkEntity();
                checkPowerAgain();
            }
            catch (System.Exception e)
            {
                error(e.Message);
            }
            valid();
        }

        public void ValidateSave()
        {
            int linkCount = HtmlHelper.CheckHyperlinkCount(Entity.Excerpt);
            if (linkCount > BlogInfo.Instance().MaxCommentURLCount)
            {
                trackbackResponse(1, "Excerpt contains too many urls");
            }
            Article art = ArticleData.GetArticleByID(Entity.LogID);
            if (art.Locked == false && art.Mode == 4)
            {
                trackbackResponse(1, "Article does not exist or is locked");
            }
            TrackBack tb = TrackBackData.GetTrackBackByName(Entity.Title, Entity.Excerpt);
            if (tb != null)
            {
                trackbackResponse(1, "Trackback is already saved");
            }
        }

        public void ValidateAdd(CallValid valid, CallError error)
        {
            try
            {
                checkPower();
                checkEntity();
                checkPowerAgain();
            }
            catch (System.Exception e)
            {
                error(e.Message);
            }
            valid();
        }
    }
}
